const axios = require("axios");
const config = require("./config");

module.exports = {
  async fetchConstants() {
    try {
      // 循环遍历API配置
      const map = new Map();
      const list = config.list || [];
      for (let i = 0; i < list.length; i++) {
        const item = list[i];
        const response = await axios({
          method: item.api.method,
          url: item.api.url,
          headers: item.api.headers,
          timeout: 5000
        });
        if (response.data.code !== 200) {
          throw new Error(`🚨 API请求失败: ${response.data.message}`);
        } else {
          console.log(`📡 API请求成功: ${item.api.url}`);
          map.set(item.name, response.data.result);
        }
      }
      return map;
    } catch (error) {
      console.error("🚨 常量获取失败:", error.message);
      throw new Error("CONSTANTS_FETCH_FAILED");
    }
  }
};
